package com.sky.controller.admin;

import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDish")
@RequestMapping("/admin/dish")
@Api(tags = "设置菜品接口")
@Slf4j
public class DishController {
    @Autowired
    public DishService dishService;
    // TODO 写一个清理redis缓存的方法
    // 当对菜品做出修改的时候清理redis的缓存

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation(value = "菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后续定义
        return Result.success(pageResult);
    }

    /**
     *新增菜品
     *
     * @param dishVO
     * @return
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public Result<String> add(@RequestBody DishVO dishVO){
        String key = "dish_"+dishVO.getCategoryId();
        cleanCache(key);
        log.info("新增菜品信息: {}", dishVO);
        dishService.addDish(dishVO);
        return Result.success();
    }

    /**
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("起售停售菜品")
    @PostMapping("/status/{status}")
    public Result<Object> startOrStop(@PathVariable Integer status, long id){
        log.info("起售停售菜品id: {}", id);
        cleanCache("dish_*");
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改菜品信息
     *
     * @param dishVO
     * @return
     */
    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result<Object> editDishMessage(@RequestBody DishVO dishVO){
        cleanCache("dish_*");
        log.info("修改菜品信息: {}", dishVO);
        dishService.edit(dishVO);
        return Result.success();
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result<Object> deleteDish(long[] ids){
        cleanCache("dish_*");
        dishService.deleteById(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id回显菜品信息")
    public Result<DishVO> getByID(@PathVariable long id){
        DishVO dishvo = dishService.getById(id);
        return Result.success(dishvo);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId){
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .build();
        List<DishVO> list = dishService.list(dish);
        return Result.success(list);
    }
}
